package DataStructures.BinaryTree;

/**
 * @author ：ALi
 * @date ：Created in 2021/11/28 20:44
 * @description：顺序存储二叉树
 * @modified By：
 * @version: $
 */
public class ArrayBinaryTree {
    public static void main(String[] args) {
        int[] array = {1,2,3,4,5,6,7};
        ArrBinaryTree tree = new ArrBinaryTree(array);
        tree.preOrder(0);

    }

}

class ArrBinaryTree {
    private int[] arr;

    public ArrBinaryTree(int[] arr) {
        this.arr = arr;
    }

    public ArrBinaryTree() {
    }

    /**
     * 前序打印,中序和后序打印就是把里面的代码换下位置，在此就不写了
     * @param num
     */
    public void preOrder(int num) {
        if (this.arr == null || this.arr.length == 0) {
            System.out.println("数组为空");
        }
        System.out.print(this.arr[num]+" ");
        if (num * 2 + 1 < this.arr.length) {
            preOrder(num * 2 + 1);
        }
        if (num * 2 + 2 < this.arr.length) {
            preOrder(num * 2 + 2);
        }
    }
}